package com.khyr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.khyr.dao.DeptDao;
import com.khyr.dao.HisPatientDao;
import com.khyr.dao.HisRegistrationDao;
import com.khyr.dao.UserDao;
import com.khyr.entity.Dept;
import com.khyr.entity.HisPatient;
import com.khyr.entity.HisRegistration;
import com.khyr.entity.User;
import com.khyr.service.RegisteredListService;
import com.khyr.vo.FindSchedulingListVo;
import com.khyr.vo.QMyPageDto;
import com.khyr.vo.QMyPageDtoDto;
import com.khyr.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @author ：Zhang
 * @date ：Created in 2025/3/14 14:28
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class RegisteredListServiceImpl implements RegisteredListService {
    @Autowired
    private DeptDao deptDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private HisPatientDao hisPatientDao;
    @Autowired
    private HisRegistrationDao hisRegistrationDao;
    /**
     * @create by: Zr
     * @description: 通过科室id，患者名，时间，时段，类型，挂号单状态查询挂号单
     * @create time: 2025/3/14 14:50
     * @param data
     * @return com.khyr.vo.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.khyr.entity.HisRegistration>>
     */
    @Override
    public R<Page<HisRegistration>> queryRegistrationForPage(FindSchedulingListVo data) {
        QueryWrapper<HisRegistration> queryWrapper = new QueryWrapper<>();
        // 科室id
        if(data.getDeptId()!=null){
            queryWrapper.eq("dept_id",data.getDeptId());
        }
        // 患者姓名
        if(data.getPatientName() != null){
            queryWrapper.like("patient_name",data.getPatientName());
        }
        // 挂号状态
        if(data.getRegStatus()!=null){
            queryWrapper.eq("registration_status",data.getRegStatus());
        }
        // 挂号类型
        if(data.getSchedulingType() != null){
            queryWrapper.eq("scheduling_type",data.getSchedulingType());
        }
        // 挂号时段
        if(data.getSubsectionType()!=null){
            queryWrapper.eq("subsection_type",data.getSubsectionType());
        }
        // 挂号日期
        if(data.getVisitDate()!=null){
            queryWrapper.eq("visit_date",data.getVisitDate());
        }
        Page<HisRegistration> page = new Page<>(Long.valueOf(data.getPageNum()), Long.valueOf(data.getPageSize()));
        Page<HisRegistration> p = hisRegistrationDao.selectPage(page, queryWrapper);
        return new R<>(200,"查询成功",p);
    }
    /**
     * @create by: Zr
     * @description: 根据挂号流水将挂号信息转为退号状态
     * @create time: 2025/3/14 14:58
     * @param registrationId
     * @return com.khyr.vo.R<java.lang.String>
     */
    @Override
    public R<String> doReturn(String registrationId) {
        QueryWrapper<HisRegistration> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("registration_id",registrationId);
        HisRegistration hisRegistration = hisRegistrationDao.selectOne(queryWrapper);
        // 修改挂号单状态为退号
        hisRegistration.setRegistrationStatus("4");
        hisRegistrationDao.updateById(hisRegistration);
        return new R<>(200,"退号成功",null);
    }
    /**
     * @create by: Zr
     * @description: 根据挂号流水将挂号信息转为作废状态
     * @create time: 2025/3/14 15:04
     * @param registrationId
     * @return com.khyr.vo.R<java.lang.String>
     */
    @Override
    public R<String> doInvalid(String registrationId) {
        QueryWrapper<HisRegistration> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("registration_id",registrationId);
        HisRegistration hisRegistration = hisRegistrationDao.selectOne(queryWrapper);
        // 修改挂号单状态为作废
        hisRegistration.setRegistrationStatus("5");
        hisRegistrationDao.updateById(hisRegistration);
        return new R<>(200,"作废成功",null);
    }
    /**
     * @create by: Zr
     * @description: 根据手机号获得用户信息,根据患者id获得挂号信息,根据传入的类型查询，由于前端传入了分页数据，但是前端未做分页，为了正常显示，因此查询固定10000条数据，但保留分页查询
     * @create time: 2025/3/18 10:31
     * @param page
     * @param pageSize
     * @param type
     * @param uId
     * @return com.khyr.vo.R<com.khyr.vo.QMyPageDto>
     */
    @Override
    public R<QMyPageDto> selectRegistrationListByStatus(Integer page, Integer pageSize, String type, String uId) {
        // 根据患者id获得挂号信息
        QueryWrapper<HisRegistration> queryWrapperHR = new QueryWrapper<>();
        queryWrapperHR.eq("patient_id",uId);
        List<HisRegistration> list = new ArrayList<>(0);
        // 前端传有分页数据，但是未作分页，因此查询固定10000条数据，但保留分页查询
        Page<HisRegistration> pages = new Page<>(1, 10000);
        QMyPageDto qMyPageDto = new QMyPageDto();
        QMyPageDtoDto qMyPageDtoDto = new QMyPageDtoDto();
        // 全部
        if("0".equals(type)){
            // 获得挂号单信息
            Page<HisRegistration> p = hisRegistrationDao.selectPage(pages, queryWrapperHR);
            qMyPageDto.setItems(getPage(p));
            qMyPageDto.setCounts(p.getTotal());
            qMyPageDto.setPages(p.getPages());
            qMyPageDto.setPageSize(Long.valueOf(pageSize));
            qMyPageDto.setPage(Long.valueOf(page));
            if(p != null){
                return new R<>(200,"查询成功",qMyPageDto);
            }else {
                return new R<>(500,"查询失败",null);
            }
        }
        // 待收费
        if("1".equals(type)){
            queryWrapperHR.eq("registration_status","0");
            Page<HisRegistration> p = hisRegistrationDao.selectPage(pages, queryWrapperHR);
            qMyPageDto.setItems(getPage(p));
            qMyPageDto.setCounts(p.getTotal());
            qMyPageDto.setPages(p.getPages());
            qMyPageDto.setPageSize(Long.valueOf(pageSize));
            qMyPageDto.setPage(Long.valueOf(page));
            if(p != null){
                return new R<>(200,"查询成功",qMyPageDto);
            }else {
                return new R<>(500,"查询失败",null);
            }
        }
        // 待就诊
        if("2".equals(type)){
            queryWrapperHR.eq("registration_status","1");
            Page<HisRegistration> p = hisRegistrationDao.selectPage(pages, queryWrapperHR);
            qMyPageDto.setItems(getPage(p));
            qMyPageDto.setCounts(p.getTotal());
            qMyPageDto.setPages(p.getPages());
            qMyPageDto.setPageSize(Long.valueOf(pageSize));
            qMyPageDto.setPage(Long.valueOf(page));
            if(p != null){
                return new R<>(200,"查询成功",qMyPageDto);
            }else {
                return new R<>(500,"查询失败",null);
            }
        }
        // 就诊中
        if("3".equals(type)){
            queryWrapperHR.eq("registration_status","2");
            Page<HisRegistration> p = hisRegistrationDao.selectPage(pages, queryWrapperHR);
            qMyPageDto.setItems(getPage(p));
            qMyPageDto.setCounts(p.getTotal());
            qMyPageDto.setPages(p.getPages());
            qMyPageDto.setPageSize(Long.valueOf(pageSize));
            qMyPageDto.setPage(Long.valueOf(page));
            if(p != null){
                return new R<>(200,"查询成功",qMyPageDto);
            }else {
                return new R<>(500,"查询失败",null);
            }
        }
        // 完成
        if("4".equals(type)){
            queryWrapperHR.eq("registration_status","3");
            Page<HisRegistration> p = hisRegistrationDao.selectPage(pages, queryWrapperHR);
            qMyPageDto.setItems(getPage(p));
            qMyPageDto.setCounts(p.getTotal());
            qMyPageDto.setPages(p.getPages());
            qMyPageDto.setPageSize(Long.valueOf(pageSize));
            qMyPageDto.setPage(Long.valueOf(page));
            if(p != null){
                return new R<>(200,"查询成功",qMyPageDto);
            }else {
                return new R<>(500,"查询失败",null);
            }
        }
        // 已退号
        if("5".equals(type)){
            queryWrapperHR.eq("registration_status","4");
            Page<HisRegistration> p = hisRegistrationDao.selectPage(pages, queryWrapperHR);
            qMyPageDto.setItems(getPage(p));
            qMyPageDto.setCounts(p.getTotal());
            qMyPageDto.setPages(p.getPages());
            qMyPageDto.setPageSize(Long.valueOf(pageSize));
            qMyPageDto.setPage(Long.valueOf(page));
            if(p != null){
                return new R<>(200,"查询成功",qMyPageDto);
            }else {
                return new R<>(500,"查询失败",null);
            }
        }
        return new R<>(500,"查询失败",null);
    }
    /**
     * @create by: Zr
     * @description: 通过查询的挂号信息获取医生信息和部门信息
     * @create time: 2025/3/18 11:29
     * @param data
     * @return com.khyr.vo.QMyPageDtoDto
     */
    private  List<QMyPageDtoDto> getPage(Page<HisRegistration> data) {
        List<QMyPageDtoDto> items = new ArrayList<>();
        for(HisRegistration hisRegistration : data.getRecords()){
            QMyPageDtoDto qMyPageDtoDto = new QMyPageDtoDto();
            // 存入挂号单信息
            qMyPageDtoDto.setRegistration(hisRegistration);
            // 获得医生信息,判断是否有医生接诊
            if(hisRegistration.getUserId()!=null){
                QueryWrapper<User> queryWrapperU = new QueryWrapper<>();
                queryWrapperU.eq("user_id",hisRegistration.getUserId());
                User user = userDao.selectOne(queryWrapperU);
                // 只获得医生姓名和头像
                User user1 = new User();
                user1.setUserName(user.getUserName());
                user1.setPicture(user.getPicture());
                // 存入医生信息
                qMyPageDtoDto.setUser(user1);
            }
            // 查询部门信息
            QueryWrapper<Dept> queryWrapperD = new QueryWrapper<>();
            queryWrapperD.eq("dept_id",hisRegistration.getDeptId());
            Dept dept = deptDao.selectOne(queryWrapperD);
            // 只获得部门名称和id
            Dept dept1 = new Dept();
            dept1.setDeptId(dept.getDeptId());
            dept1.setDeptName(dept.getDeptName());
            qMyPageDtoDto.setDept(dept1);
            items.add(qMyPageDtoDto);
        }
        return items;
    }
}
